package tool.utils;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;

/**
 * 从文件中获取char流
 */
public class CharStreamUtils {

    public static CharStream getStream(String fileName) {
        CharStream input = null;
        String files = "";
        try {
            files = CharStreams.fromFileName(fileName).toString();
            String newStr = "";
            String str[] = files.split("\n");
            for (String string : str) {
				if(string.trim().startsWith("#define")){
					if(!string.contains(";")){
						string = "";//舍去该行定义，如#define long long long
					}else{
						String str2[] = string.trim().split(" ");
						if(str2.length>1) string = "define "+str2[1] + " {";
						for(int i = 2; i < str2.length; i++){
							string = string + str2[i];
						}
						string = string + "}";
					}
				}
				
				newStr = newStr + string +"\n";
			}
            input = CharStreams.fromString(newStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return input;
    }
}
